using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ICP_MS.Plugin.Tune.Properties;

namespace ICP_MS.Plugin.Tune;

public class TurnCntlBak : UserControl
{
	private volatile bool bSendValue = false;

	private int decimalPlaces = 0;

	private decimal increment = 1m;

	private decimal minValue = default(decimal);

	private decimal maxValue = default(decimal);

	private decimal _value = default(decimal);

	private IContainer components = null;

	private NumericUpDown nudValue;

	private Label lblText;

	private Button btnSend;

	private PictureBox ledFeed;

	[Description("显示小数点位数，仅仅为显示")]
	[Category("Parameters")]
	public int DecimalPlaces
	{
		get
		{
			return decimalPlaces;
		}
		set
		{
			decimalPlaces = value;
			nudValue.DecimalPlaces = decimalPlaces;
		}
	}

	[Description("单步值，有效小数位数需小于控件小数点位数")]
	[Category("Parameters")]
	public decimal Increment
	{
		get
		{
			return increment;
		}
		set
		{
			increment = value;
			nudValue.Increment = value;
			MinValue = minValue;
			MaxValue = maxValue;
			Value = _value;
		}
	}

	[Description("最小值")]
	[Category("Parameters")]
	public decimal MinValue
	{
		get
		{
			return minValue;
		}
		set
		{
			minValue = value;
			nudValue.Minimum = minValue;
		}
	}

	[Description("最大值")]
	[Category("Parameters")]
	public decimal MaxValue
	{
		get
		{
			return maxValue;
		}
		set
		{
			maxValue = value;
			nudValue.Maximum = maxValue;
		}
	}

	[Description("当前值")]
	[Category("Parameters")]
	public decimal Value
	{
		get
		{
			return _value;
		}
		set
		{
			_value = value;
			nudValue.Value = value;
		}
	}

	[Description("控件显示名")]
	[Category("Parameters")]
	[Browsable(true)]
	public string Label
	{
		get
		{
			return lblText.Text;
		}
		set
		{
			lblText.Text = value;
		}
	}

	[Description("线性转换为十六进制的斜率")]
	[Category("Parameters")]
	public double K { get; set; }

	[Description("线性转换为十六进制的截距")]
	[Category("Parameters")]
	public double C { get; set; }

	[Description("CAN目标ID")]
	[Category("Parameters")]
	public uint DestID { get; set; }

	[Description("代码ID，标识自身")]
	[Category("Parameters")]
	public uint CodeID { get; set; }

	public event EventHandler ValueChanged;

	public TurnCntlBak()
	{
		InitializeComponent();
		ApplyResources();
	}

	private void ApplyResources()
	{
		btnSend.Text = Lang.Txt("TurnCntlFeed.btnSend.Text");
	}

	private void btnSend_Click(object sender, EventArgs e)
	{
		Task.Factory.StartNew(delegate
		{
			Invoke((EventHandler)delegate
			{
				ledFeed.Image = Resources.led_Green;
			});
			Thread.Sleep(500);
			Invoke((EventHandler)delegate
			{
				ledFeed.Image = Resources.led_Gray;
			});
		});
		_value = nudValue.Value;
		if (this.ValueChanged != null)
		{
			this.ValueChanged(this, null);
		}
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.nudValue = new System.Windows.Forms.NumericUpDown();
		this.lblText = new System.Windows.Forms.Label();
		this.btnSend = new System.Windows.Forms.Button();
		this.ledFeed = new System.Windows.Forms.PictureBox();
		((System.ComponentModel.ISupportInitialize)this.nudValue).BeginInit();
		((System.ComponentModel.ISupportInitialize)this.ledFeed).BeginInit();
		base.SuspendLayout();
		this.nudValue.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.nudValue.Location = new System.Drawing.Point(83, 3);
		this.nudValue.Name = "nudValue";
		this.nudValue.Size = new System.Drawing.Size(70, 21);
		this.nudValue.TabIndex = 1;
		this.nudValue.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
		this.lblText.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.lblText.Location = new System.Drawing.Point(3, 0);
		this.lblText.Name = "lblText";
		this.lblText.Size = new System.Drawing.Size(74, 28);
		this.lblText.TabIndex = 2;
		this.lblText.Text = "label";
		this.lblText.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
		this.btnSend.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.btnSend.Location = new System.Drawing.Point(83, 28);
		this.btnSend.Name = "btnSend";
		this.btnSend.Size = new System.Drawing.Size(69, 23);
		this.btnSend.TabIndex = 3;
		this.btnSend.Text = "Tx";
		this.btnSend.UseVisualStyleBackColor = true;
		this.btnSend.Click += new System.EventHandler(btnSend_Click);
		this.ledFeed.Image = ICP_MS.Plugin.Tune.Properties.Resources.led_Gray;
		this.ledFeed.Location = new System.Drawing.Point(5, 30);
		this.ledFeed.Name = "ledFeed";
		this.ledFeed.Size = new System.Drawing.Size(20, 20);
		this.ledFeed.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
		this.ledFeed.TabIndex = 6;
		this.ledFeed.TabStop = false;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.ledFeed);
		base.Controls.Add(this.btnSend);
		base.Controls.Add(this.lblText);
		base.Controls.Add(this.nudValue);
		base.Name = "TurnCntl";
		base.Size = new System.Drawing.Size(153, 55);
		((System.ComponentModel.ISupportInitialize)this.nudValue).EndInit();
		((System.ComponentModel.ISupportInitialize)this.ledFeed).EndInit();
		base.ResumeLayout(false);
	}
}
